<?php
class Model_Admin_Country extends Model{
    public function getInfo(){

        $sql="select t.countryid id,t.name,t.createdon,t.createdby
             from country t
             where isdeleted<>'1'
             order by t.CreatedOn desc";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;
    }
    public  function saveInfo($id,$name){

        if($id==""){
            return $this->add($name);
        }
        else{
            return $this->alter($id,$name);
        }

    }
    private function add($name){
        try {

            list($insert_id, $total_rows)= DB::insert('country')

            ->columns(array('name','createdby','createdon'))
                
            ->values(array($name,Session::instance()

            ->get('admin_username',NULL),date("Y-m-d h:i:sa")))->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$name){

        try {
            $total_rows=DB::update('country')

            ->set(array('name'=>$name))

            ->where('countryid','=',$id)->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
}